package bootTest.controller;


import bootTest.mapper.TestMapper;
import bootTest.redis.JedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

@Controller
@RequestMapping("test")
public class TestController extends BaseController {


    @Autowired
    private JedisUtils jedisUtils;

    @Autowired
    private TestMapper testMapper;

    @RequestMapping("testEvent")
    @ResponseBody
    public String showHelloWorld(HttpServletResponse response, String param, String arrayParam) throws Exception {


        testMapper.getDataById("asdsad");
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("B");
        list.add("C");

        jedisUtils.set("test", list);


        try {
            ArrayList test = (ArrayList) jedisUtils.get("test");
            System.out.println(Arrays.toString(test.toArray()));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "asdsad";
    }

    @RequestMapping("page")
    public String showPage() {
        return "adsa";
    }

    @PostMapping("apitest")
    @ResponseBody
    public String apitest(@RequestBody Map map) {
        return map == null ? "空" : map.toString();
    }

    @PostMapping("login")
    @ResponseBody
    public String login(String name, String pass) throws Exception {
        Map data = testMapper.getDataByPass(name, pass);
        if (data == null) {
            return fail("login fail");
        }
        jedisUtils.set(data.get("id").toString(), data);
        return success("login success啊");
    }

    public static void main(String[] args) {
        String s = "dsadsad%s";
        String dasd = String.format(s, "啊");
        System.out.println(dasd);
    }
}
